从php页面中的java DataOutputStream接收输出
我有一个java小程序,我正在使用它将文件发送回我的服务器——在服务器端,我希望在php页面上接收这个文件
下面是执行发送的java代码,在php方面,我检查了全局数组,我有URL传递的数据,但没有文件数据。我真的在这一条上搜索和抓取了,所以感谢任何帮助
String strURL = sendToURL + "?ACTION=POST&LEN=" + imgBytes.length + "&Fname=picture.png";
try{
URL urlServlet = new URL(strURL);
URLConnection sCon = urlServlet.openConnection();
sCon.setDoInput(true);
sCon.setDoOutput(true);
if (sCon.getAllowUserInteraction()) {
sCon.setAllowUserInteraction(true);
}
sCon.setUseCaches(false);
sCon.setDefaultUseCaches(false);
sCon.setRequestProperty("Content-Type", "text/html");
sCon.setRequestProperty("Connection", "Keep-Alive");
sCon.setConnectTimeout(transferTimeout);
sCon.setReadTimeout(transferTimeout);
DataOutputStream out = new DataOutputStream(sCon.getOutputStream());
int index = 0;
size = 1024;
do {
if (index + size > imgBytes.length) {
size = imgBytes.length - index;
}
out.write(imgBytes, index, size);
index += size;
} while (index < imgBytes.length);
out.write(imgBytes);
out.flush();
out.close();
解决了——正如经常发生的那样,一个人在经过几天的斗争后发布一个问题,仅仅几分钟后就会出现一个解决方案
我记得之前有一次使用cURL传输XML数据,在听到关于使用SOAP的评论后,我开始思考这个问题。几次搜索之后,我发现了一个更简单、更优雅的解决方案
http://www.lornajane.net/posts/2008/Accessing-Incoming-PUT-Data-from-PHP
基本上,您可以使用
file_get_contents("php://input")
所以现在它的效果非常好
# 1 楼答案
我多次使用Soap消息将数据从PHP传输到Java,效果良好
因此,使用PHP作为Web服务,并通过SOAP进行通信
设置WSDL文件
生成Java存根和骨架 http://download.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html
将WSDL加载到php脚本中 http://www.php.net/manual/de/book.soap.php
在php中执行逻辑
然后使用Java存根调用服务器并传输数据